#!/usr/bin/env python
# coding: utf-8

# In[1]:


import datetime
import requests

class EtigerInternalDataClient:
    def __init__(self, base_url):
        self.__base_url = base_url
        self.__taos_bar_daily_instrument_url = self.__base_url + '/finance/taos/bar/daily/instrument'
        self.__taos_bar_daily_query_url = self.__base_url + '/finance/taos/bar/daily/query'
        self.__taos_bar_minute_instrument_url = self.__base_url + '/finance/taos/bar/minute/instrument'
        self.__taos_bar_minute_query_url = self.__base_url + '/finance/taos/bar/minute/query'
        self.__taos_tick_instrument_url = self.__base_url + '/finance/taos/tick/instrument'
        self.__taos_tick_query_url = self.__base_url + '/finance/taos/tick/query'
    def __parse_json_response(self, rsp):
        code = rsp['code']
        msg = rsp['msg']
        trace_id = rsp['traceId']
        if '0' == code:
            return rsp['data']
        else:
            print("[%s][ERROR] EtigerInternalDataClient response error, trace_id [%s]" %(datetime.datetime.now(), trace_id))
            raise Exception(msg)
    def get_taos_bar_daily_instrument(self):
        '''
        查询taos日k线合约
        '''
        rsp = requests.get(self.__taos_bar_daily_instrument_url, verify=True)
        rsp = rsp.json()
        return self.__parse_json_response(rsp)
    def taos_bar_daily_query(self, batch_list, key_list):
        '''
        查询taos日k线数据
        '''
        payload = {
            'batchList': batch_list,
            'keyList': key_list
        }
        rsp = requests.post(self.__taos_bar_daily_query_url, json=payload, verify=True)
        rsp = rsp.json()
        return self.__parse_json_response(rsp)
    def get_taos_bar_minute_instrument(self):
        '''
        查询taos分钟k线合约
        '''
        rsp = requests.get(self.__taos_bar_minute_instrument_url, verify=True)
        rsp = rsp.json()
        return self.__parse_json_response(rsp)
    def taos_bar_minute_query(self, batch_list, key_list):
        '''
        查询taos分钟k线数据
        '''
        payload = {
            'batchList': batch_list,
            'keyList': key_list
        }
        rsp = requests.post(self.__taos_bar_minute_query_url, json=payload, verify=True)
        rsp = rsp.json()
        return self.__parse_json_response(rsp)
    def get_taos_tick_instrument(self):
        '''
        查询taos tick合约
        '''
        rsp = requests.get(self.__taos_tick_instrument_url, verify=True)
        rsp = rsp.json()
        return self.__parse_json_response(rsp)
    def taos_tick_query(self, batch_list):
        '''
        查询taos tick数据
        '''
        payload = {
            'batchList': batch_list
        }
        rsp = requests.post(self.__taos_tick_query_url, json=payload, verify=True)
        rsp = rsp.json()
        return self.__parse_json_response(rsp)

def example_get_taos_bar_daily_instrument(internal_data_client):
    instrument_list = internal_data_client.get_taos_bar_daily_instrument()
    print("[%s][INFO] get_taos_bar_daily_instrument [%s]" %(datetime.datetime.now(), instrument_list))

def example_taos_bar_daily_query(internal_data_client):
    batch_list = [
        {
            "instrumentIdList":[
                "sh601318",
                "sz000001",
                "sh600036"
            ],
            "tradingDateBegin":"2021-06-01",
            "tradingDateEnd":"2021-06-10"
        },
        {
            "instrumentIdList":[
                "sh600143"
            ],
            "tradingDateBegin":"2021-05-01",
            "tradingDateEnd":"2021-05-10"
        }
    ]
    key_list = [
        {
            "tradingDate":"2021-06-17",
            "instrumentId":"sh601318"
        },
        {
            "tradingDate":"2021-06-18",
            "instrumentId":"sz000001"
        }
    ]
    bar_list = internal_data_client.taos_bar_daily_query(batch_list, key_list)
    print("[%s][INFO] taos_bar_daily_query [%s]" %(datetime.datetime.now(), bar_list))

def example_get_taos_bar_minute_instrument(internal_data_client):
    instrument_list = internal_data_client.get_taos_bar_minute_instrument()
    print("[%s][INFO] get_taos_bar_minute_instrument [%s]" %(datetime.datetime.now(), instrument_list))

def example_taos_bar_minute_query(internal_data_client):
    batch_list = [
        {
            "instrumentIdList":[
                "sh601318",
                "sz000001",
                "sh600036"
            ],
            "barTimeBegin":"2021-06-01 00:00",
            "barTimeEnd":"2021-06-10 20:00"
        },
        {
            "instrumentIdList":[
                "sh600143"
            ],
            "barTimeBegin":"2021-05-01 00:00",
            "barTimeEnd":"2021-05-10 20:00"
        }
    ]
    key_list = [
        {
            "barTime":"2021-06-17 20:00",
            "instrumentId":"sh601318"
        },
        {
            "barTime":"2021-06-18 20:00",
            "instrumentId":"sz000001"
        }
    ]
    bar_list = internal_data_client.taos_bar_minute_query(batch_list, key_list)
    print("[%s][INFO] taos_bar_minute_query [%s]" %(datetime.datetime.now(), bar_list))

def example_get_taos_tick_instrument(internal_data_client):
    instrument_list = internal_data_client.get_taos_tick_instrument()
    print("[%s][INFO] get_taos_tick_instrument [%s]" %(datetime.datetime.now(), instrument_list))

def example_taos_tick_query(internal_data_client):
    batch_list = [
        {
            "instrumentIdList":[
                "sh601318",
                "sz000001",
                "sh600036"
            ],
            "tickTimeBegin":"2021-06-01 10:00:00",
            "tickTimeEnd":"2021-06-01 10:05:00"
        },
        {
            "instrumentIdList":[
                "sh600143"
            ],
            "tickTimeBegin":"2021-07-01 13:00:00",
            "tickTimeEnd":"2021-07-01 13:05:00"
        }
    ]
    tick_list = internal_data_client.taos_tick_query(batch_list)
    print("[%s][INFO] taos_tick_query [%s]" %(datetime.datetime.now(), tick_list))

if __name__ == '__main__':
    base_url = 'http://192.168.10.125:20001/etiger/internal/data/api/'
    internal_data_client = EtigerInternalDataClient(base_url)
    example_get_taos_bar_daily_instrument(internal_data_client)
    example_taos_bar_daily_query(internal_data_client)
    example_get_taos_bar_daily_instrument(internal_data_client)
    example_taos_bar_minute_query(internal_data_client)
    example_get_taos_tick_instrument(internal_data_client)
    example_taos_tick_query(internal_data_client)
